<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="工法名称" prop="gfName">
        <el-input size="mini"
                  v-model="queryParams.gfName"
                  placeholder="请输入工法名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="nd">
        <el-input size="mini" type="number"
                  v-model="queryParams.nd"
                  placeholder="请输入年度"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工法类别" prop="gfLb">
        <el-select @change="getZyFlQuery" size="mini" v-model="queryParams.gfLb" placeholder="请选择工法类别" clearable>
          <el-option
            v-for="dict in dict.type.qygf_gflb"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业分类" prop="zyFl">
        <el-select v-if="queryParams.gfLb == null" size="mini" v-model="queryParams.zyFl" placeholder="请先选择工法类别"
                   clearable>
        </el-select>
        <el-select v-if="queryParams.gfLb == '房屋建筑工程'" size="mini" v-model="queryParams.zyFl"
                   placeholder="请选择专业分类" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyfl_fwjzgc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select v-if="queryParams.gfLb == '土木工程'" size="mini" v-model="queryParams.zyFl"
                   placeholder="请选择专业分类" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyfl_tmgc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        <el-select v-if="queryParams.gfLb == '工业安装工程'" size="mini" v-model="queryParams.zyFl"
                   placeholder="请选择专业分类" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyfl_gyazgc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申报级别" prop="sbJb">
        <el-select size="mini" v-model="queryParams.sbJb" placeholder="请选择申报级别" clearable>
          <el-option
            v-for="dict in dict.type.qygf_sbjb"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工法完成人" prop="zyWcR">
        <el-input size="mini"
                  v-model="queryParams.zyWcR"
                  placeholder="请输入工法完成人"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报单位" prop="sbDw">
        <el-select size="mini" v-model="queryParams.sbDw" placeholder="请选择申报单位" clearable>
          <el-option
            v-for="dict in dict.type.xmgl_lxwj_pzdw"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="专业领域" prop="zyLy">
        <el-select size="mini" v-model="queryParams.zyLy" placeholder="请选择专业领域" clearable>
          <el-option
            v-for="dict in dict.type.qygf_zyly"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.qygf_lczt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kjcg:qygf:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
          @click="handleUpdate"
          v-hasPermi="['kjcg:qygf:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
          @click="handleDelete"
          v-hasPermi="['kjcg:qygf:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['kjcg:qygf:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
          @click="handleStartTask"
        >发起申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-delete"
          @click="handleRevokeProcess"
          :disabled="!selectedRow || selectedRow.processStatus !== 2"
        >撤销流程</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="qygfList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="年度" align="center" prop="nd" width="70"/>
      <el-table-column label="工法名称" align="center" prop="gfName">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.gfName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="工法编号" align="center" prop="gfBh" />
      <el-table-column label="申报级别" align="center" prop="sbJb">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qygf_sbjb" :value="scope.row.sbJb"/>
        </template>
      </el-table-column>
      <el-table-column label="工法类别" align="center" prop="gfLb">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qygf_gflb" :value="scope.row.gfLb"/>
        </template>
      </el-table-column>
      <el-table-column label="专业分类" align="center" prop="zyFl"/>
      <el-table-column label="工法完成人" align="center" prop="zyWcR"/>
      <el-table-column label="申报单位" align="center" prop="sbDw">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xmgl_lxwj_pzdw" :value="scope.row.sbDw"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="processStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qygf_lczt" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.processStatus==1 || scope.row.processStatus==4"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业工法对话框 -->
    <vxe-modal :title="title" v-model="open" width="80%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="95px">
        <el-tabs tab-position="right" type="border-card" v-model="activeName">
          <el-tab-pane name="jbzl" label="基本资料">
            <div v-if="activeName === 'jbzl'" style="height: 73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff">基本资料</span>
                </div>
<!--                <el-form-item label="工法编号" prop="gfBh">-->
<!--                  <el-input v-model="form.gfBh" placeholder="请输入工法编号" />-->
<!--                </el-form-item>-->
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="工法名称" prop="gfName">
                      <el-input v-model="form.gfName" placeholder="请输入工法名称"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="年度" prop="nd">
                      <el-input type="number" v-model="form.nd" placeholder="请输入年度"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="工法类别" prop="gfLb">
                      <el-select @change="getFormByGfFl" v-model="form.gfLb" placeholder="请选择工法类别">
                        <el-option
                          v-for="dict in dict.type.qygf_gflb"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="专业分类" prop="zyFl">
                      <el-select v-if="form.gfLb == null" v-model="form.zyFl" placeholder="请先选择工法类别">
                      </el-select>
                      <el-select v-if="form.gfLb == '房屋建筑工程'" v-model="form.zyFl" placeholder="请选择专业分类">
                        <el-option
                          v-for="dict in dict.type.qygf_zyfl_fwjzgc"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                      <el-select v-if="form.gfLb == '土木工程'" v-model="form.zyFl" placeholder="请选择专业分类">
                        <el-option
                          v-for="dict in dict.type.qygf_zyfl_tmgc"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                      <el-select v-if="form.gfLb == '工业安装工程'" v-model="form.zyFl" placeholder="请选择专业分类">
                        <el-option
                          v-for="dict in dict.type.qygf_zyfl_gyazgc"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="申报级别" prop="sbJb">
                      <el-select v-model="form.sbJb" placeholder="请选择申报级别">
                        <el-option
                          v-for="dict in dict.type.qygf_sbjb"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="工法完成人" prop="zyWcR">
                      <el-input v-model="form.zyWcR" placeholder="请选择工法完成人" @focus="selectUser(form.kjryId)"
                                :suffix-icon="'el-icon-search'"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="联系人" prop="lxr">
                      <el-input v-model="form.lxr" placeholder="请输入联系人"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="联系电话" prop="lxDh">
                      <el-input v-model="form.lxDh" placeholder="请输入联系电话"/>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="联系QQ" prop="lxQq">
                      <el-input v-model="form.lxQq" placeholder="请输入联系QQ"/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="申报单位" prop="sbDw">
                      <el-select v-model="form.sbDw" placeholder="请选择申报单位">
                        <el-option
                          v-for="dict in dict.type.xmgl_lxwj_pzdw"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="24">
                  <el-col :span="12">
                    <el-form-item label="专业领域" prop="zyLy">
                      <el-select v-model="form.zyLy" placeholder="请选择专业领域">
                        <el-option
                          v-for="dict in dict.type.qygf_zyly"
                          :key="dict.value"
                          :label="dict.label"
                          :value="dict.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-form-item label="科研项目" prop="kyXm">
                  <el-input v-model="form.kyXm" placeholder="请选择科研项目" @focus="selectKt()"
                            :suffix-icon="'el-icon-search'"/>
                </el-form-item>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="qy" label="一、前言">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">前言</span>
                </div>
                <editor v-model="form.qy" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="gftd" label="二、工法特点">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">工法特点</span>
                </div>
                <editor v-model="form.gfTd" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="syfw" label="三、适用范围">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">适用范围</span>
                </div>
                <editor v-model="form.syFw" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="gyyl" label="四、工艺原理">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">工艺原理</span>
                </div>
                <editor v-model="form.gyYl" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="sggylcJCzyd" label="五、施工工艺流程及操作要点">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">施工工艺流程及操作要点</span>
                </div>
                <editor v-model="form.sggylcCzyd" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="clYSb" label="六、材料与设备">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">材料与设备</span>
                </div>
                <editor v-model="form.clSb" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="zlkz" label="七、质量控制">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">质量控制</span>
                </div>
                <editor v-model="form.zlKz" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="aqcs" label="八、安全措施">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">安全措施</span>
                </div>
                <editor v-model="form.aqCs" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="hbcs" label="九、环保措施">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">环保措施</span>
                </div>
                <editor v-model="form.hbCs" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="xyfx" label="十、效益分析">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">效益分析</span>
                </div>
                <editor v-model="form.xyFx" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane name="yysl" label="十一、应用实例">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">应用实例</span>
                </div>
                <editor v-model="form.yySl" :height="500" :min-height="300"/>
              </el-card>
            </div>
          </el-tab-pane>

          <el-tab-pane v-if="form.idWhere != null" name="fjzl" label="附件资料">
            <div style="height:73vh; overflow-y: auto;">
              <el-card class="box-card">
                <div slot="header" class="clearfix" style="text-align: center;">
                  <span style="font-size: 20px; color: #1890ff;">附件资料</span>
                  <br>
                  <span v-if="qygfFileType == '工法申报表'"
                        style="font-size: 20px; color: #1890ff;">工法申报表(盖章上传)</span>
                  <span v-if="qygfFileType == '工法文本'" style="font-size: 20px; color: #1890ff;">工法文本</span>
                  <span v-if="qygfFileType == '省(部)级工法批准文件、工法证书'" style="font-size: 20px; color: #1890ff;">省(部)级工法批准文件、工法证书</span>
                  <span v-if="qygfFileType == '省(部)级工法评审委员会的评审意见'"
                        style="font-size: 20px; color: #1890ff;">省(部)级工法评审委员会的评审意见(包括关键技术评价)</span>
                  <span v-if="qygfFileType == '工程应用证明'" style="font-size: 20px; color: #1890ff;">工程应用证明(建设单位或监理单位出具的工法应用证明原件、施工许可证或开工报告、工程施工合同复印件)</span>
                  <span v-if="qygfFileType == '经济效益证明'"
                        style="font-size: 20px; color: #1890ff;">经济效益证明</span>
                  <span v-if="qygfFileType == '无争议声明书'" style="font-size: 20px; color: #1890ff;">无争议声明书(包括所有工法完成单位、主要完成人，涉及使用专利等)</span>
                  <span v-if="qygfFileType == '科技查新报告'"
                        style="font-size: 20px; color: #1890ff;">科技查新报告</span>
                  <span v-if="qygfFileType == '关键技术专利和科技成果获奖证书'"
                        style="font-size: 20px; color: #1890ff;">关键技术专利和科技成果获奖证书</span>
                  <span v-if="qygfFileType == '工法应用照片10-15张'" style="font-size: 20px; color: #1890ff;">工法应用照片10-15张</span>
                  <span v-if="qygfFileType == '企业工法证书及批文'"
                        style="font-size: 20px; color: #1890ff;">企业工法证书及批文</span>
                </div>

                <el-row :gutter="20">
                  <el-col :span="6" :xs="24">
                    <div class="head-container">
                      <el-tree
                        :data="fileOptions"
                        :props="defaultProps"
                        :expand-on-click-node="false"
                        :filter-node-method="filterNode"
                        ref="tree"
                        node-key="id"
                        default-expand-all
                        highlight-current
                        @node-click="handleNodeClick"
                        :render-content="renderContent"
                      >
                      </el-tree>
                    </div>
                  </el-col>
                  <el-col :span="18" :xs="24">
                    <el-row :gutter="10" class="mb8">
                      <el-col :span="1.5">
                        <!-- 上传按钮 -->
                        <el-button size="mini" icon="el-icon-upload" type="primary" @click="qygfFileUpload">上传
                        </el-button>
                      </el-col>
                      <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQygfFj">删除
                        </el-button>
                      </el-col>
                    </el-row>
                    <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                              :data="qygfFjList" :row-class-name="rowQygfFjIndex"
                              @selection-change="handleQygfFjSelectionChange" height="48vh"
                              ref="qygfFj" v-loading="loadingFj">
                      <el-table-column type="selection" width="50" align="center"/>
                      <el-table-column label="序号" align="center" prop="index" width="50"/>
                      <el-table-column show-overflow-tooltip label="文件类型" align="center" prop="fileType"/>
                      <el-table-column show-overflow-tooltip label="文件名称" align="center" prop="fileName" :formatter="getFileNames"/>
                      <el-table-column show-overflow-tooltip label="文件大小" align="center" prop="fileSize"/>
                      <el-table-column label="操作" align="center" width="150">
                        <template slot-scope="scope">
                          <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-download"
                            @click="handleDownload(scope.row)"
                          >下载
                          </el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-col>
                </el-row>
              </el-card>
            </div>
          </el-tab-pane>

        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 多选科技人员 -->
    <KjryDuoXName ref="kjryDuoXName" @resSubmit="resSubmit"/>

    <!-- 科研项目选择 -->
    <HtXzKt ref="ktXz"/>

    <!-- 执行填报附件上传弹窗 -->
    <vxe-modal title="附件上传" v-model="fjUpload" width="25%" showFooter show-zoom resize @close="fjClose">
      <el-upload
        align="center"
        class="upload-demo"
        multiple
        :action="uploadFileUrl"
        :before-upload="handleBeforeUpload"
        :file-list="fileList"
        :limit="20"
        :on-error="handleUploadError"
        :on-exceed="handleExceed"
        :on-success="handleUploadSuccess"
        :headers="headers"
        ref="fileUpload"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">
          请上传
          <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
          <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
          的文件
        </div>
      </el-upload>

      <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
        <el-button v-if="allFilesUploaded === false" size="mini" type="primary" @click="fjUpload = false">关 闭
        </el-button>
        <el-button v-if="allFilesUploaded === true" size="mini" type="primary" @click="fjClose">上传完成, 关闭
        </el-button>
      </div>

    </vxe-modal>

    <!-- 查询进度 -->
    <Jindu ref="picture"/>

    <!-- 企业工法详情 -->
    <QyGfXq ref="qyGfXq"/>
  </div>
</template>

<script>
import {
  addQygf, deleteQygfFjByFjIds,
  delQygf,
  getQygf,
  getQyGfId,
  insertQygfFjByQygfId,
  listQygf,
  selectQygfFjList,
  updateQygf
} from "@/api/kjcg/qygf";
import KjryDuoXName from "@/views/components/kjryXz/kjryDuoXName.vue";
import HtXzKt from "@/views/components/ktXz/HtXzKt.vue";
import {getToken} from "@/utils/auth";
import Jindu from "@/views/components/liuChengState/jindu.vue";
import {revokeProcess, startTask} from "@/api/activiti/task";
import QyGfXq from "@/views/kjcg/qygfsb/components/QyGfXq.vue";

export default {
  name: "Qygfsb",
  components: {QyGfXq, Jindu, HtXzKt, KjryDuoXName},
  //企业工法申报
  dicts: ['xmgl_lxwj_pzdw', 'qygf_sbjb', 'qygf_zyly', 'qygf_gflb', 'qygf_zyfl_fwjzgc', 'qygf_zyfl_tmgc', 'qygf_zyfl_gyazgc', 'qygf_lczt'],
  watch: {
    activeName(newVal) {
      if (newVal === 'fjzl') {
        this.clickFirstNode();
      }
    },
  },
  mounted() {
    if (this.activeName === 'fjzl') {
      this.clickFirstNode();
    }
  },
  provide() {
    return {
      //赋值给 工法完成人
      updateKtZcRyLabel: (val) => {
        // 使用 map() 方法遍历数组中的每个对象，并提取出 name 属性
        const names = val.map(item => item.name);
        const ids = val.map(item => item.kjryId)

        // 使用 join() 方法将名字数组转换为逗号分隔的字符串
        this.form.zyWcR = names.join(',')
        this.form.kjryId = ids.join(',')
        this.$refs['formDialog'].validateField('zyWcR'); // formDialog 是表单的 ref，zyWcr 是要验证的字段名
      },
      updateXmsbId: (val) => {
        //科研项目
        this.form.kyXm = '【' + val.ktBh + '】' + val.ktName
        //项目申报ID
        this.form.xmsbId = val.xmsbId
        this.$refs['formDialog'].validateField('kyXm'); // formDialog 是表单的 ref，zyWcr 是要验证的字段名
      }
    }
  },
  data() {
    return {
      loadingFj: false,
      // 附件查询参数
      queryParamsFj: {
        pageNum: 1,
        pageSize: 100,
        qygfId: null,
        fileType: null,
        fileName: null,
        fileSize: null
      },
      qygfFileType: "工法申报表", //默认的附件类型
      fileOptions: [//附件类型树表
        {id: 1, label: '1.工法申报表', type: '工法申报表'},
        {id: 2, label: '2.工法文本', type: '工法文本'},
        {id: 3, label: '3.省(部)级工法批准文件、工法证书', type: '省(部)级工法批准文件、工法证书'},
        {
          id: 4,
          label: '4.省(部)级工法评审委员会的评审意见',
          type: '省(部)级工法评审委员会的评审意见'
        },
        {
          id: 5,
          label: '5.工程应用证明',
          type: '工程应用证明'
        },
        {id: 6, label: '6.经济效益证明', type: '经济效益证明'},
        {
          id: 7,
          label: '7.无争议声明书',
          type: '无争议声明书'
        },
        {id: 8, label: '8.科技查新报告', type: '科技查新报告'},
        {id: 9, label: '9.关键技术专利和科技成果获奖证书', type: '关键技术专利和科技成果获奖证书'},
        {id: 10, label: '10.工法应用照片10-15张', type: '工法应用照片10-15张'},
        {id: 11, label: '11.企业工法证书及批文', type: '企业工法证书及批文'},
      ],
      defaultProps: {children: "children", label: "label"}, // 表单参数
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      //要修改的 企业工法id
      qygfIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      getQyGfId: null, //从后端获取到的企业工法ID (UUID)
      activeName: 'jbzl', //初始打开的标配页
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQygfFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业工法表格数据
      qygfList: [],
      // 企业工法附件表格数据
      qygfFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gfName: null,
        nd: null,
        gfLb: null,
        zyFl: null,
        sbJb: null,
        zyWcR: null,
        kjryId: null,
        sbDw: null,
        zyLy: null,
        xmsbId: null,
        kyXm: null,
        idWhere: null,
        userId: this.$store.state.user.id,
        deptId: null,
        processStatus: null,
        gfBh: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gfName: [{required: true, message: "工法名称不能为空", trigger: "blur"}],
        nd: [{required: true, message: "年度不能为空", trigger: "blur"}],
        gfLb: [{required: true, message: "工法类别不能为空", trigger: "change"}],
        zyFl: [{required: true, message: "专业分类不能为空", trigger: "change"}],
        sbJb: [{required: true, message: "申报级别不能为空", trigger: "change"}],
        zyWcR: [{required: true, message: "工法完成人不能为空", trigger: "blur"}],
        sbDw: [{required: true, message: "申报单位不能为空", trigger: "change"}],
        zyLy: [{required: true, message: "专业领域不能为空", trigger: "change"}],
        kyXm: [{required: true, message: "科研项目不能为空", trigger: "blur"}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //打开企业工法详情
    handleButtonClick(row) {
      this.$refs.qyGfXq.handleOpen(row);
    },
    //发起任务
    handleStartTask() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确认发起申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$modal.loading("正在发起申请，请稍后...");
          startTask({ businessKey: row.qygfId, businessType: 12 }).then(res => {
            this.getList();
            this.$modal.msgSuccess("发起成功");
            this.selectedRow = null
            // 关闭遮罩层
            this.$modal.closeLoading();
          });
        })
        .catch(() => {});
    },
    //撤销流程
    handleRevokeProcess() {
      const row = this.selectedRow; // 获取当前选中的行
      if (!row) {
        this.$message.error("请先选择一行数据");
        return;
      }
      this.$confirm("是否确撤销申请？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$modal.loading("正在撤销申请，请稍后...");
          let params = {
            businessKey: row.qygfId,
            businessType: 12,
            processInstanceId: row.processInstanceId
          };
          revokeProcess(params).then(res => {
            this.getList();
            this.$modal.msgSuccess("撤销成功");
            this.selectedRow = null
            // 关闭遮罩层
            this.$modal.closeLoading();
          });
        })
        .catch(() => {});
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    clickFirstNode() {
      this.$nextTick(() => {
        const firstNode = this.$refs.tree.store.nodesMap['1']; // 默认点击id为1的节点
        if (firstNode) {
          this.$refs.tree.setCurrentKey(firstNode.key);
          this.handleNodeClicks(firstNode.data);
        }
      });
    },
    handleNodeClicks(data) {
      this.loadingFj = true
      this.qygfFileType = data.type
      this.queryParamsFj.fileType = this.qygfFileType
      selectQygfFjList(this.queryParamsFj).then(resFileList => {
        this.qygfFjList = resFileList.rows;
        this.loadingFj = false
      })
    },
    // 节点单击事件
    handleNodeClick(data, node) {
      this.loadingFj = true
      if (node.level > 0 && node.data.children && node.data.children.length > 0) { // 检查是否有子节点
        const firstChildNode = node.data.children[0]; // 获取第一个子节点
        this.$refs.tree.setCurrentKey(firstChildNode.id); // 设置当前选中的节点为第一个子节点
        data = firstChildNode; // 更新 data 为第一个子节点的数据
        this.qygfFileType = data.type
        this.queryParamsFj.fileType = this.qygfFileType
        selectQygfFjList(this.queryParamsFj).then(resFileList => {
          this.qygfFjList = resFileList.rows;
          this.loadingFj = false
        })
      } else {
        //根据附件类型查询附件列表
        this.qygfFileType = data.type
        this.queryParamsFj.fileType = this.qygfFileType
        selectQygfFjList(this.queryParamsFj).then(resFileList => {
          this.qygfFjList = resFileList.rows;
          this.loadingFj = false
        })
      }
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    //关闭上传附件弹窗后
    fjClose() {
      this.fileList = []
      this.allFilesUploaded = false
      this.fjUpload = false
    },
    //上传文件之前的钩子
    handleBeforeUpload(file) {
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let xzItemFile = {
          "qygfId": that.getQyGfId,
          "fileType": that.qygfFileType,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        let xgItemFile = {
          "qygfId": that.qygfIdEdit,
          "fileType": that.qygfFileType,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        if (this.xg === false) {
          // 点击新增按钮的上传
          insertQygfFjByQygfId(xzItemFile).then(resFil => {
            that.getQygfFjList();
          })
        } else if (this.xg === true) {
          // 点击修改按钮的上传
          insertQygfFjByQygfId(xgItemFile).then(resFil => {
            that.getQygfFjList();
          })
        }
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.allFilesUploaded = true
      this.$modal.msgSuccess("上传成功")
      // this.fjShangChuan = false
    },
    getQygfFjList() {
      let that = this;
      if (this.xg === false) {
        //点击新增 上传文件后的文件列表
        this.queryParamsFj.qygfId = that.getQyGfId
        this.queryParamsFj.fileType = this.qygfFileType
        selectQygfFjList(this.queryParamsFj).then(resFileList => {
          that.qygfFjList = resFileList.rows;
        })
      } else {
        //点击修改 上传文件后的文件列表
        this.queryParamsFj.qygfId = that.qygfIdEdit
        this.queryParamsFj.fileType = this.qygfFileType
        selectQygfFjList(this.queryParamsFj).then(resFileList => {
          that.qygfFjList = resFileList.rows;
        })
      }
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.fileList = this.fileList.map(item => {
          // 使用split方法去除name中的特定前缀
          item.name = item.name.split('/').pop();
          return item;
        });
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    //企业工法附件上传按钮
    qygfFileUpload() {
      this.fjUpload = true;
    },
    // 打开选择科研项目弹窗
    selectKt() {
      this.$refs.ktXz.handleOpen();
    },
    resSubmit(ids) {
      this.form.kjryId = ids
    },
    //打开科技人员多选组件(科技人员姓名)(张三,李四,王五,赵六)
    selectUser(kjryId) {
      this.$refs.kjryDuoXName.handleOpen(kjryId);
    },
    //当搜索栏中更换工法类别时, 重置专业分类
    getZyFlQuery() {
      this.queryParams.zyFl = null
    },
    //当表单中更换工法类别时, 重置专业分类
    getFormByGfFl() {
      this.form.zyFl = null
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询企业工法列表 */
    getList() {
      this.loading = true;
      listQygf(this.queryParams).then(response => {
        this.qygfList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qygfId: this.getQyGfId,
        gfName: null,
        nd: new Date().getFullYear(),
        gfLb: null,
        zyFl: null,
        sbJb: null,
        zyWcR: null,
        kjryId: null,
        lxr: null,
        lxDh: null,
        lxQq: null,
        sbDw: null,
        zyLy: null,
        xmsbId: null,
        kyXm: null,
        qy: null,
        gfTd: null,
        syFw: null,
        gyYl: null,
        sggylcCzyd: null,
        clSb: null,
        zlKz: null,
        aqCs: null,
        hbCs: null,
        xyFx: null,
        yySl: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,
        gfBh: null
      };
      this.qygfFjList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.qygfId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.xg = false
      getQyGfId().then(res => {
        this.getQyGfId = res.msg
        this.reset();
      })
      this.activeName = 'jbzl'
      this.open = true;
      this.title = "添加企业工法";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      this.xg = true
      const qygfId = row.qygfId || this.ids
      this.qygfIdEdit = row.qygfId || this.ids
      getQygf(qygfId).then(response => {
        this.form = response.data;

        //点击新增 上传文件后的文件列表
        this.queryParamsFj.qygfId = qygfId
        this.queryParamsFj.fileType = this.qygfFileType
        selectQygfFjList(this.queryParamsFj).then(resFileList => {
          this.qygfFjList = resFileList.data;
        })
        this.activeName = 'jbzl'
        this.open = true;
        this.title = "修改企业工法";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.form.qygfFjList = this.qygfFjList;
          if (this.form.idWhere != null) { //修改
            updateQygf(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.xg = false
            });
          } else { //新增
            addQygf(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.handleUpdate(this.form)
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qygfIds = row.qygfId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delQygf(qygfIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 企业工法附件序号 */
    rowQygfFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 企业工法附件删除按钮操作 */
    handleDeleteQygfFj() {
      if (this.checkedQygfFj.length == 0) {
        this.$modal.msgError("请先选择要删除的企业工法附件数据");
      } else {
        const fjIds = this.checkedQygfFj;
        this.$modal.confirm('确定删除选中记录？').then(function() {
          return deleteQygfFjByFjIds(fjIds);
        }).then(() => {
          this.queryParamsFj.qygfId = this.qygfIdEdit || this.getQyGfId
          this.queryParamsFj.fileType = this.qygfFileType
          selectQygfFjList(this.queryParamsFj).then(resFileList => {
            this.qygfFjList = resFileList.rows;
            console.log(this.qygfFjList, 'dsaadsdasd')
            this.loadingFj = false
          })
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    },
    /** 复选框选中数据 */
    handleQygfFjSelectionChange(selection) {
      this.checkedQygfFj = selection.map(item => item.fjId)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kjcg/qygf/export', {
        ...this.queryParams
      }, `qygf_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
